/**
 * 邮件账号列表
 **/
function loadMailAccountData(){
    $("#jqGrid").jqGrid($.extend(jqGrids.defaults,{
        url: base+'/mail/account/view/mailAccountList',
        colNames:['发邮件系统CODE','邮箱地址','登录名','编码','邮箱类型','<div align="center">操作</div>'],
        colModel: [
            {name:'appCode',index:'appCode',sortable:false},
            {name:'host',index:'host',sortable:false},
            {name:'username',index:'username',sortable:false},
            {name:'defaultEncoding',index:'defaultEncoding'},
            {name:'mailAccountType',index:'mailAccountType'},
            {name:'id',index:' ',width:'208',align:'center',sortable:false,formatter : function(value, options, rData){
                return "<a href='javascript:void(0)' onclick=doMailAccountEdit(\""+value+"\")><span class='btn btn-sm purple'>编辑</span></a>";
            }}
        ]
    }));
}
/**
 * 邮件列表
 **/
function loadData(){
    $("#jqGrid").jqGrid($.extend(jqGrids.defaults,{
        url: base+'/mail/info/view/list',
        colNames:['发邮件系统CODE','发件方','接收方','邮件主题','邮件正文','邮件发送时间','邮件类型'],
        colModel: [
            {name:'appCode',index:'appCode',sortable:false},
            {name:'from',index:'from',sortable:false},
            {name:'sendTo',index:'sendTo',sortable:false},
            {name:'subject',index:'subject'},
            {name:'content',index:'content'},
            {name:'sendTime',index:'sendTime',formatter:tools.fomDate},
            {name:'mailType',index:'mailType'}
        ],
        rowNum: 100,
        rowList : [100,150,200],
        multiselect: false
    }));
}
/**
 * 邮件模板列表
 **/
function loadTemplateData(){
    $("#jqGrid").jqGrid($.extend(jqGrids.defaults,{
        url: base+'/mail/template/view/list',
        colNames:['发邮件系统CODE','发件方','接收方','邮件主题','邮件正文','邮件发送时间','邮件类型'],
        colModel: [
            {name:'appCode',index:'appCode',sortable:false},
            {name:'from',index:'from',sortable:false},
            {name:'sendTo',index:'sendTo',sortable:false},
            {name:'subject',index:'subject'},
            {name:'content',index:'content'},
            {name:'sendTime',index:'sendTime',formatter:tools.fomDate},
            {name:'mailType',index:'mailType'}
        ]
    }));
}

/**邮件查询*/
function doSearch(){
    var subject = $("input[name=subject]").val();
    var startTime = $("input[name=startTime]").val();
    var endTime = $("input[name=endTime]").val();
    var mailType = $("select[name=mailType]").val();
    $("#jqGrid").jqGrid('setGridParam',{
        datatype:'json',
        postData:{'subject':subject,'startTime':startTime,'endTime':endTime,'mailType':mailType}, //发送数据
        page:1
    }).trigger("reloadGrid"); //重新载入
}
/**邮件账号查询*/
function doMailAccountSearch(){
    var mailBoxType = $("input[name=mailBoxType]").val();
    var appCode = $("select[name=appCode]").val();
    $("#jqGrid").jqGrid('setGridParam',{
        datatype:'json',
        postData:{'mailAccountType':mailBoxType,'appCode':appCode}, //发送数据
        page:0
    }).trigger("reloadGrid"); //重新载入
}

function doMailAccountAdd(id) {
    var url =base+ "/mail/account/uc/edit?id=" + id + "&type=add";
    window.location.href=url;
}
function doMailAccountEdit(id) {
    var url =base+ "/mail/account/uc/edit?id=" + id + "&type=edit";
    window.location.href=url;
}

function doMailTemplateAdd() {
    var url =base+ "/mail/template/sk/add";
    window.location.href=url;
}

/**
 *mailaccount删除
 */
function doMailTemplateDelete() {
    var ids=$('#jqGrid').jqGrid('getGridParam','selarrrow');
    if(ids.length<=0){
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    var url =base+"/mail/template/dh/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if(ck){
            $.post(url, {ids: ids}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
                if (data && data.code == 'SUCCESS') {
                    $("#jqGrid").trigger("reloadGrid");
                }
            });
        }})
}

/**
 *mailaccount删除
 */
function doMailAccountDelete() {
    var ids=$('#jqGrid').jqGrid('getGridParam','selarrrow');
    if(ids.length<=0){
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    var url =base+"/mail/account/dh/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if(ck){
            $.post(url, {ids: ids}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
                if (data && data.code == 'SUCCESS') {
                    $("#jqGrid").trigger("reloadGrid");
                }
            });
        }})
}

function doMailAccountSubmit(type) {
    if (!formCheck().form()) {
        return;
    }
    var url;

    if ("add" == type) {
        url = base + "/mail/account/sk/insert";
    } else {
        url = base + "/mail/account/uc/update";
    }
    $.post(url, $("#editForm").serialize(), function (data) {
        $.messager.alert0({selector: "#editAlert", type: data.code, html: data.message});
        if (data && data.code == 'SUCCESS') {
            tools.goto(base + "/mail/account/view/index", 500);
        }
    }, "json");

}


function formCheck() {
    return $('#editForm').validate({
        rules: {
            appCode: {
                required:true
            },
            username: {
                required:true
            },
            host: {
                // email:true
                required:true
            },
            password: {
                required:true
            },
            description: {
                maxlength:[100]
            }
        }
    });
}


